<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function getMenuLink($menuItem) {
    $render = get_post_meta($menuItem->ID, 'render', true);
    if(!empty($render)) {
        return get_bloginfo('url').$render;
    } else {
        return get_permalink($menuItem->ID);
    }
}

global $post;
// najdi najvyssieho parenta aktualnej stranky
$p = $post;
while ($p->post_parent != 0) {
    $p = get_page($p->post_parent);
}

// najdi najvyssie polozene stranky
$tops = get_pages(array('parent'=>0, 'sort_column'=>'menu_order'));

//najdi ktory je aktualny
$current = 0;
foreach($tops as $top) {
    if ($top->ID == $p->ID) {
        $current = $top->ID;
    }
}

// ak nenasiel, tak je to stranka s clankami
if ($current == 0) {
    $current = get_option('page_for_posts');
}
?>
<div id="mainMenu">
    <ul class="ul-level1">
        <?php
        foreach ($tops as $top) {
            echo '<li class="headlink">';
            // zvyrazni aktivnu cast menu
            echo '<a href="'.getMenulink($top).'" class="headlink-a'.($top->ID==$current?'-current':'').'">';
            echo $top->post_title;
            echo '</a>';
            echo '<ul class="ul-level2">';
            foreach (get_pages(array('child_of'=>$top->ID, 'parent'=>$top->ID)) as $sub) {
                echo '<li class="sublink">';
                echo '<a href="'.getMenuLink($sub).'">';
                echo $sub->post_title;
                echo '</a>';
                echo '<ul class="ul-level3">';
                $i = 0;
                foreach (get_pages(array('child_of' => $sub->ID, 'parent' => $sub->ID)) as $subsub) {
                    echo '<li class="subsublink">';
                    echo '<a href="'.getMenulink($subsub).'">';
                    echo $subsub->post_title;
                    echo '</a>';
                    echo '</li>';
                }
                echo '</ul>';
                echo '</li>';
            }
            echo '</ul>';
            echo '</li>';
        }
        ?>
    </ul>
    <?php get_search_form();
    ?>
</div>
